local likePython = {"python", "sh", "ruby", "yaml", "makefile"}
local likeC = {"c", "cpp", "java", "javascript", "scala", "typescript", "verilog", "systemverilog", "rust"}

function contain(table, value)
  for _, v in ipairs(table) do
    if v == value then
      return true
    end
  end
  return false
end

function AddFileHeader()
  -- 获取文件信息
  local filepath = vim.fn.expand('%:p')  -- 完整文件路径
  local filename = vim.fn.expand('%:t')  -- 文件名
  local filetype = vim.bo.filetype       -- 文件类型

  -- 配置信息（可以放在 vim.g 全局变量中）
  local author = vim.g.my_author_name or "LYM"
  local email = vim.g.my_author_email or "2640076547@qq.com"

  -- 日期时间格式
  local create_date = os.date('%Y-%m-%d')
  local create_time = os.date('%H:%M:%S')
  local last_edit_time = os.date('%Y-%m-%d %H:%M:%S')

  -- 根据文件类型确定注释风格
  local comment_open, comment_close, comment_line = "", "", ""
  if filetype == "lua" then
    comment_line = "-- "
  elseif contain(likePython, filetype) then
    comment_line = "# "
  elseif filetype == "vim" then
    comment_line = "\" "
  elseif contain(likeC, filetype) then
    comment_open = "/*"
    comment_line = " * "
    comment_close = " */"
  elseif filetype == "html" or filetype == "xml" then
    comment_open = "<!--"
    comment_line = "  "
    comment_close = "-->"
  else
    comment_line = "# "  -- 默认使用 # 注释
  end

  -- 构建文件头内容
  local header = {}

  -- 添加注释开始标记
  if comment_open ~= "" then
    table.insert(header, comment_open)
  end

  -- 添加文件头信息
  table.insert(header, comment_line .. "File: " .. filename)
  -- table.insert(header, comment_line .. "Path: " .. filepath)  -- 不需要文件路径
  table.insert(header, comment_line .. "Author: " .. author .. " <" .. email .. ">")
  table.insert(header, comment_line .. "Date: " .. create_date .. " " .. create_time)
  table.insert(header, comment_line .. "LastEditTime: " .. last_edit_time)
  table.insert(header, comment_line .. "Description: ")
  -- 添加注释结束标记
  if comment_close ~= "" then
    table.insert(header, comment_close)
  end

  -- 添加空行分隔
  table.insert(header, "")

  -- 插入到文件开头
  vim.api.nvim_buf_set_lines(0, 0, 0, false, header)

  -- 移动光标到描述位置
  local last_line = #header - 1  -- 描述所在行
  vim.api.nvim_win_set_cursor(0, {last_line, #comment_line + 12})
end

-- 设置快捷键
vim.keymap.set('n', '<space>ha', AddFileHeader, {desc = "Add file header"})

local function get_comment_style(ft)
  if ft == "lua" then
    return "", "", "-- "
  elseif contain(likePython, ft) then
    return "", "", "# "
  elseif ft == "vim" then
    return "", "", "\" "
  elseif contain(likeC, ft) then
    return "/*", " */", " * "
  elseif ft == "html" or ft == "xml" then
    return "<!--", "-->", "  "
  else
    return "", "", "# "
  end
end

-- 更新文件头中的最后编辑时间
local function update_header_last_edit_time()
  -- 只有当缓冲区有修改时才更新
  if not vim.bo.modified then
    return
  end

  local filetype = vim.bo.filetype
  local comment_open, comment_close, comment_line = get_comment_style(filetype)

  -- 构建搜索模式（匹配最后编辑时间行）
  local pattern = vim.pesc(comment_line) .. "LastEditTime: .*"

  -- 搜索前20行（文件头通常位于文件开头）
  for lnum = 1, 20 do
    local line = vim.fn.getline(lnum)
    if line:match(pattern) then
      -- 找到匹配行，更新为当前时间
      local new_line = comment_line .. "LastEditTime: " .. os.date("%Y-%m-%d %H:%M:%S")
      vim.fn.setline(lnum, new_line)
      break
    end
  end
end

-- 设置保存时自动更新最后编辑时间
vim.api.nvim_create_autocmd("BufWritePre", {
  pattern = "*",
  callback = update_header_last_edit_time,
})
